import { order } from '../model/order'
import { orderDetail } from '../model/orderDetail'
import {User} from '../model/user'
export class submitController {
  // 返回当前表所有的数据的方法
  async orders() {
    return order.findAll()
  }
  async orderDetails() {
    return orderDetail.findAll()
  }

  async createOrder(payload) {
    let { username, totalPrice } = payload
    // 获取username对应的用户id
    let user=await User.findOne({
      where: {username}
    })
    let UserId = user.id
    // 插入订单语句
    const InsertOrderRes = await order.create({
      totalPrice,
      payTime:new Date(),
      dinningTableId:1,
      UserId
    })
    console.log('插入订单返回结果',InsertOrderRes.dataValues)

    // 插入订单详情
    let foodOrder = payload.order
    for (let foodItem of foodOrder) {
      let {count,foodName} = foodItem
      let foodId = foodItem.id
      await orderDetail.create({
        count,
        foodId,
        foodName,
        orderId: InsertOrderRes.dataValues.id,
      })
    }
  }
}
